const { sequelize, Config } = require('../models');
const { Op } = require('sequelize');

class ConfigService {
	static injector(ctn) {
		return [
			ctn.make('varal.app')
		]
	}
	constructor(app) {
		this.app = app;
	}

	async info() {
		return Config.findOne({
			where: {
				id: { [Op.gt]: 0 }
			}
		})
	}

	async save(params) {
		['create_time', 'update_time', 'delete_time'].forEach((d) => {
			delete params[d];
		});

		const { id } = await Config.findOne({
			where: {
				id: { [Op.gt]: 0 }
			}
		}).then((data) => data || ({}));

		if (id) {
			return Config.update(params, {
				where: { id }
			})
		} else {
			return Config.create(params);
		}
	}
}

exports = module.exports = ConfigService;
